package com.xindaoapp.happypet.ui;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.XinDaoBaseAdapter;
import com.xindaoapp.happypet.bean.DaShangRecord;
import com.xindaoapp.happypet.fragments.UserInfoFragment;
import com.xindaoapp.happypet.utils.BitmapUtil;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.view.CircleImageView;

/**
 * 打赏记录
 * 
 * @author Stone
 * 
 */
public class DaShangRecordActivity extends BaseActivity {

    private PullToRefreshListView mPullToRefreshListView;
    private DSListAdapter mAdapter;
    private RelativeLayout mNoData;
    private Context mContext;
    private String mTid, mPid;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_da_shang_record;
    }

    @Override
    protected void initViews() {

        mTid = getIntent().getStringExtra(Constants.KEY_TID);
        mPid = getIntent().getStringExtra(Constants.KEY_PID);

        mContext = this;

        mPullToRefreshListView = (PullToRefreshListView) this.findViewById(R.id.listview);
        mPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                onLoadDatas();
            }
        });

        super.initViews();
    }



    @Override
    protected void initEvents() {
        super.initEvents();
    }


    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();

        getData();
    }


    private void getData() {

        getMoccaApi().getDaShangRecord(mTid, mPid, Constants.PAGE_FIRST, Constants.PAGE_SIZE,
                new IRequest<DaShangRecord>() {

                    @Override
                    public void request(DaShangRecord t) {



                        if (t != null) {
                            if (t.array.size() == 0) {
                                onDataArrivedEmpty();
                            } else {
                                onDataArrived(true);
                                mAdapter =
                                        new DSListAdapter(DaShangRecordActivity.this, t.array,
                                                Constants.PAGE_SIZE, R.layout.item_dashangrecord,
                                                R.layout.item_loading);
                                mPullToRefreshListView.setAdapter(mAdapter);
                            }

                        } else {
                            onDataArrived(false);
                        }
                        mPullToRefreshListView.onRefreshComplete();
                    }
                });

    }


    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        };
    }


    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }


    @Override
    protected String getTopBarTitleStrRes() {

        return "打赏记录";
    }

    /**
     * 打赏列表适配器
     * 
     * @author Stone
     * 
     */

    class DSListAdapter extends XinDaoBaseAdapter<DaShangRecord.DaShangInfo> {

        public DSListAdapter(Context context, List<DaShangRecord.DaShangInfo> iniData,
                int pageSize, int res, int loadingRes) {
            super(context, iniData, pageSize, res, loadingRes);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent,
                final DaShangRecord.DaShangInfo value) {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                holder.iv_img = (CircleImageView) convertView.findViewById(R.id.iv_img);
                holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
                holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                holder.tv_reason = (TextView) convertView.findViewById(R.id.tv_reason);
                holder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
                convertView.setTag(holder);
            }


            ImageLoader.getInstance().displayImage(value.userface, holder.iv_img,
                    BitmapUtil.getPersonLoading());
            holder.tv_name.setText(value.username);
            if (value.reason.equals("")) {
                holder.tv_reason.setVisibility(View.GONE);
            } else {
                holder.tv_reason.setVisibility(View.VISIBLE);
                holder.tv_reason.setText(value.reason);
            }

            holder.tv_count.setText(value.score);
            holder.tv_time.setText(CommonUtil.formatTimeForListCommonForDaShang(value.dateline));

            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    // 进入他人中心
                    Intent intent =
                            new Intent(mContext, com.xindaoapp.happypet.activity.MainActivity.class);
                    intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                            UserInfoFragment.class.getCanonicalName());
                    Bundle bundle = new Bundle();
                    bundle.putString(Constants.KEY_UID, value.uid);
                    bundle.putString(Constants.KEY_USERNAME, value.username);
                    bundle.putString(Constants.KEY_USERFACE, value.userface);
                    intent.putExtra(Constants.KEY_BUNDLE, bundle);
                    startActivity(intent);
                }
            });

            return convertView;
        }

        class ViewHolder {
            CircleImageView iv_img;
            TextView tv_time, tv_reason, tv_name, tv_count;
        }
    }

}
